package servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import Database.StaffDB;
import Model.Approval;
import Model.Committee;
import Model.CommitteeRole;

/**
 * Servlet implementation class CommitteeTable
 */

public class CommitteeTable3 extends HttpServlet {
	private static final long serialVersionUID = 1L;
	JspWriter out;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CommitteeTable3() {
		super();
		// TODO Auto-generated constructor stub
	}

	public CommitteeTable3(JspWriter outputStream) {
		// TODO Auto-generated constructor stub
		out = outputStream;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		ArrayList<Committee> commTable = new ArrayList<Committee>();
		commTable = Database.CommitteeDB.getAllCommitteesUnApproved();

		ArrayList<CommitteeRole> commRole = new ArrayList<CommitteeRole>();

		for (int i = 0; i < commTable.size(); i++) {

			out.print("<tr>");

			out.print("<td>");
			out.print("<input type =\"checkbox\" name=\"check\" value=\""
					+ commTable.get(i).getId() + "\" />");
			out.print("</td>");

			out.print("<td>");
			out.print(commTable.get(i).getTitle());
			out.print("</td>");

			out.print("<td>");
			out.print(commTable.get(i).getPurpose());
			out.print("</td>");

			commRole = Database.CommitteeRoleDB.getCommitteeRole(commTable.get(
					i).getId());
			out.print("<td>");
			System.out.println("commRole size is  : " + commRole.size());
			if(commRole.size() == 0)
			{
				System.out.println("Offsetting by two columns");
				out.print("</td><td></td>");
			}
			for (int j = 0; j < commRole.size(); j++) {
			
				if((commRole.get(j).getName() == null && commRole.get(j).getStaffName() != null) || (commRole.get(j).getName().isEmpty() && commRole.get(j).getStaffName().isEmpty() == false))
				{
					System.out.println("Offsetting by one column");
					out.println("<td></td>");
				}
				System.out.println("Comm Role is : " + commRole.get(j).getName());
				out.print(commRole.get(j).getName() + "&nbsp;&nbsp;<br>");
				if(commRole.size() == 1 || j == commRole.size() - 1)
				{
				out.print("</td><td>");
				}
				else
				{
					out.print(commRole.get(j + 1).getName() + "&nbsp;&nbsp;");
				}
				String[] arr = commRole.get(j).getStaffName().split(", ");
				for (int k = 0; k < arr.length; k++) {
					if (k == arr.length - 1) {
						out.print(Database.StaffDB.getStaffNamebyId(arr[k]));
					} else {
						out.print(Database.StaffDB.getStaffNamebyId(arr[k])
								+ ", ");
					}
					out.print("</td");
				}
				if(arr.length == 0)
				{
					out.print("</td>");
				}
				out.print("<br>");
			}
			out.print("</td>");

			out.print("<td>");
			out.print(formatdate(commTable.get(i).getDate()));

			out.print("</td>");

			out.print("</tr>");

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	private String formatdate(String date) {
		String result = date.split(" ")[0] + " " + date.split(" ")[1].charAt(0)
				+ date.split(" ")[1].substring(1).toLowerCase() + " "
				+ date.split(" ")[2];
		return result;
	}

	private boolean checkExpire(String date) throws ParseException {
		// return false if expire
		boolean check = true;

		SimpleDateFormat dateFormat = new SimpleDateFormat("d MMM yyyy");
		Date convertedDate = dateFormat.parse(date);

		Date now = new Date();

		int diffInDays = (int) ((now.getTime() - convertedDate.getTime()) / (1000 * 60 * 60 * 24));

		int noOfDay = Integer.parseInt(Logic.SetApptExpire_BLL
				.getCurrentNumber());
		if (diffInDays > noOfDay) {
			check = false;
		}

		return check;
	}

}
